package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.ClassesList;
import com.wuseguang.springcloud.service.OrganizationalStructureService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @描述 数据管理-->组织架构
 * @创建人 zm
 * @创建时间 2019/12/11
 */
@RestController
@RequestMapping("/organizationalstructure")
public class OrganizationalStructureController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrganizationalStructureService organizationalStructureService;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 查询组织架构
     * @创建人 zm
     * @创建时间 2019/12/7
     */
    @RequestMapping("/queryorganizationalstructure")
    public Result queryOrganizationalStructure(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        return new Result(organizationalStructureService.queryOrganizationalStructure(schoolId));
    }

    /**
     * @描述 查询组织架构
     * @创建人 zm
     * @创建时间 2019/12/7
     */
    @RequestMapping("/queryeducation")
    public Result queryeDucation(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        return new Result(organizationalStructureService.queryOrganizationalStructure1(schoolId));
    }

    /**
     * @描述 批量添加 部门或年级/班级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/batchAddition")
    public Result batchAddition(RequestParameterWrapper request, @RequestParam String education_id) {//education_id 0添加部门 1添加小学部 2添加初中们  3添加高中学部
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i = organizationalStructureService.batchAddition(schoolId, education_id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 添加部门
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/adddepartment")
    public Result addDepartment(RequestParameterWrapper request, @RequestParam String departmentname) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i = organizationalStructureService.addDepartment(schoolId, departmentname);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 修改部门信息
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/updatedepartment")
    public Result updateDepartment(@RequestParam String id, @RequestParam String departmentname) {
        int i = organizationalStructureService.updateDepartment(departmentname, id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 删除部门
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/deldepartment")
    public Result delDepartment(@RequestParam String id) {
        int i = organizationalStructureService.delDepartment(id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 添加年级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/addgrade")
    public Result addGradet(RequestParameterWrapper request, @RequestParam String education_id) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i = organizationalStructureService.addGrade(schoolId, education_id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 删除年级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/delgrade")
    public Result delGrade(@RequestParam String id) {
        int i1 = organizationalStructureService.queryClass(id);
        if (i1 > 0) {
            return new Result(CodeUtil.INCLUDE_DELETE);
        }
        int i = organizationalStructureService.delGrade(id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 修改学校其他的名称
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/updategrade")
    public Result updateGrade(@RequestParam String name, @RequestParam String id) {
        int i = organizationalStructureService.updateGrade(name, id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 修改班级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/updateclass")
    public Result updateClass(@RequestParam String name, @RequestParam String id) {
        int i = organizationalStructureService.updateClass(name, id);
        if (i > 0) {
            classCardService.classesAndCards(id);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 删除班级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/delclass")
    public Result delClass(@RequestParam String id) {
        //判断班级下是否有学生
        int i1 = organizationalStructureService.queryClassCount(id);
        if (i1 > 0) {
            return new Result(CodeUtil.INCLUDE_STUDENTS);
        }
        int i = organizationalStructureService.delClass(id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 添加班级
     * @创建人 zm
     * @创建时间 2019/12/8
     */
    @RequestMapping("/addclass")
    public Result addClass(RequestParameterWrapper request, @RequestParam("name") String name, @RequestParam String grade_id) {
        String schoolId = request.getParam("schoolId");
        logger.info("schoolId={}", schoolId);
        int i = organizationalStructureService.addClass(schoolId, name, grade_id);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 批量修改班级名称
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @RequestMapping("/batchupdateclass")
    public Result batchUpDateClass(@RequestBody ClassesList list) {
        if (list == null || list.getClassesList() == null || list.getClassesList().size() <= 0) {
            logger.info("batchUpDateClass--null");
            return new Result(CodeUtil.MODIFICATION_FAILED);
        }
        int i = organizationalStructureService.batchUpDateClass(list);
        if (i > 0) {
            //classCardService.classesAndCards(id);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }
}
